package spinLock;

import java.util.concurrent.atomic.AtomicReference;

//自旋锁
public class SpinLockDemo {
    AtomicReference<Thread> atomicReference = new AtomicReference<>();
    //加锁
    public void myLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "加锁");
        while (!atomicReference.compareAndSet(null, thread)){

        }
    }
    //解锁
    public void myUnLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "解锁");
        atomicReference.compareAndSet(thread, null);
    }
}
